package com.pet.server.ext.action.square;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.BeanToJson;
import com.pet.server.ext.Result;
import com.pet.server.ext.action.message.Answer;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.AnswerInfo;
import com.pet.server.ext.data.Image;
import com.pet.server.ext.data.Page;
import com.pet.server.ext.data.PetMissing;
import com.pet.server.ext.data.UserInfo;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.JsonUtil;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;

public class Missingdetail extends ActionBase{
	public Integer miss_id;
	public Page page;
	public Missingdetail(){
		 needCheckAuth =true;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		miss_id = NumberUtil.getInteger(params.getFieldValue("miss_id"));
		if(StringUtil.isEmpty(miss_id)){
			throw ApiError.Type.INVALID_PARAM.toException();
		}
		page = newPage(params);
	}
	
	@Override
	public Result invokeService() throws ApiError {
		Result result = new Result();
		JSONObject data = new JSONObject();
		UserService userService = new UserService();
		PetMissing petMissing = userService.loadPetMissing(miss_id);
		if(null==petMissing){
			throw ApiError.Type.EXIST_PARAM.toException();
		}
		UserInfo createUser = userService.findUserInfoById(petMissing.getUser_id());
		JSONObject userJson = new JSONObject();
		if(null!=createUser){
			userJson.accumulate("nick_name", createUser.nick_name);
			userJson.accumulate("user_id", createUser.user_id);
			userJson.accumulate("ico_path", createUser.ico_path);
		}
		List<Image> list= userService.queryImage(petMissing.miss_id, BaseAttribute.image_type.pet_missing.toString());
		data.accumulate("user", userJson);
		
		/**
		JSONObject missJson = new JSONObject();
		missJson.accumulate("area", petMissing.area);
		missJson.accumulate("state",petMissing.state);
		missJson.accumulate("miss_date", JsonUtil.getTime(petMissing.miss_date));
		missJson.accumulate("brand", petMissing.breed);
		missJson.accumulate("brand_name", petMissing.breed_name);
		missJson.accumulate("city", petMissing.city);
		missJson.accumulate("phone", petMissing.phone);
        JSONArray imagesJson=new JSONArray();
        for(Image image:list){
        	imagesJson.add(image.src);
        }
        missJson.accumulate("images", imagesJson);
		data.accumulate("pet_miss", missJson);
        */
        
		data.accumulate("area", petMissing.area);
		data.accumulate("state",petMissing.state);
		data.accumulate("miss_date", JsonUtil.getTime(petMissing.miss_date));
		data.accumulate("brand", petMissing.breed);
		data.accumulate("brand_name", petMissing.breed_name);
		data.accumulate("city", petMissing.city);
		data.accumulate("phone", petMissing.phone);
        JSONArray imagesJson=new JSONArray();
        for(Image image:list){
        	imagesJson.add(image.src);
        }
        data.accumulate("images", imagesJson);
		//data.accumulate("pet_miss", missJson);
		
		//JSONObject answerJson = new JSONObject();
		List<AnswerInfo> answerList = userService.queryAnswerInfo(miss_id, BaseAttribute.image_type.pet_missing.toString());
        JSONArray listJson = new JSONArray();
		for(AnswerInfo aw:answerList){
        	JSONObject awJson = new JSONObject();
        	awJson.accumulate("user_id", aw.user_id);
        	awJson.accumulate("nick_name", aw.nick_name);
        	awJson.accumulate("ico_path", aw.ico_path);
        	awJson.accumulate("create_time", JsonUtil.getTime(aw.create_time));
        	awJson.accumulate("answer_text", aw.answer_text);
        	JSONObject replyJson = null;
        	if(aw.reply_id>0){
        		AnswerInfo reply = userService.findAnswerInfo(aw.reply_id);
        		if(null!=reply){
        			replyJson=new JSONObject();
        			awJson.accumulate("user_id", reply.user_id);
                	awJson.accumulate("nick_name", reply.nick_name);
                	awJson.accumulate("ico_path", reply.ico_path);
                	awJson.accumulate("create_time", JsonUtil.getTime(reply.create_time));
                	awJson.accumulate("answer_text", reply.answer_text);
        		}
        	}
        	awJson.accumulate("replay", replyJson);
        	
        	
        	listJson.add(awJson);
        }
		data.accumulate("list", listJson);
		//data.accumulate("answer", answerJson);
		
        
        
        //userService.searchAnswerInfo(id, BaseAttribute.image_type.pet_missing.toString(),page);
        //List<AnswerInfo> list = userService.queryAnswerInfo(id, BaseAttribute.image_type.pet_missing.toString());
        //for(AnswerInfo a:list){
        //	if(a.reply_id>0){
        		//userService.findAnswerInfo(a.reply_id);
        //	}
        //}
        
        //JSONObject pm = BeanToJson.beanToJson(petMissing);
        //result.setPage(pm, page,"answer_type","oid","state");
        //pm.put("answer_list", BeanToJson.listTojson(list,"answer_id","answer_type","oid","state"));
        //o.put("answer_list", BeanToJson.listTojson(list));
		result.put("data", data);
	    return result;
	}


}
